package com.sskj.lib.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.R;
import com.sskj.lib.widget.RudessMaterialDialog;

public class TipUtil {


    /**
     * 公告弹窗
     */
    public static void showGonggao(Context context, String title, String content) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(context)
                .customView(R.layout.lib_dialog_gonggao, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);

        ((TextView) customView.findViewById(R.id.tvContent)).setText(content);
        ((TextView) customView.findViewById(R.id.tvTitle)).setText(title);
        if (TextUtils.isEmpty(title)) {
            customView.findViewById(R.id.tvTitle).setVisibility(View.GONE);
        }
        ClickUtil.click(customView.findViewById(R.id.ivClose), () -> {
            dialog.dismiss();
        });
    }

    /**
     * 各种提示弹窗
     *
     * @param activity
     * @param title
     * @param content
     * @param sure
     * @param onTipSure
     * @return
     */
    public static MaterialDialog getSureTip(Activity activity, String title, String content, String sure, OnTipSure onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_sure, false)
                .autoDismiss(true)
                .cancelable(true)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        Button btSure = customView.findViewById(R.id.btSure);
        Button btCancel = customView.findViewById(R.id.btCancel);
        ClickUtil.click(btCancel, dialog::dismiss);
        tvContent.setText(Html.fromHtml(content));
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
        }
        tvTitle.setText(title);
        btSure.setText(sure);

        ClickUtil.click(btSure, () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        return dialog;
    }

    /**
     * 账号失效弹窗，不可手动取消
     */
    public static MaterialDialog getExitTip(Activity activity, String content, OnTipSure onTipSure) {
        MaterialDialog dialog = new RudessMaterialDialog.Builder(activity)
                .customView(R.layout.lib_dialog_exit, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvContent = customView.findViewById(R.id.tvContent);
        TextView tvTitle = customView.findViewById(R.id.tvTitle);
        TextView btSure = customView.findViewById(R.id.btSure);
        ImageView ivClose = customView.findViewById(R.id.ivClose);
        ivClose.setVisibility(View.GONE);
        ClickUtil.click(ivClose, () -> {
            dialog.dismiss();
        });
        tvContent.setText(content);
        tvTitle.setText(App.INSTANCE.getString(R.string.libappCircleCheckUtil1));
        btSure.setText(App.INSTANCE.getString(R.string.libtipUtil2));
        ClickUtil.click(btSure, () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getSureTip(Activity activity, String title, String content, OnTipSure onTipSure) {
        return getSureTip(activity, title, content, App.INSTANCE.getString(R.string.libtipUtil2), onTipSure);
    }

    public static MaterialDialog getSureTip(Activity activity, String content, OnTipSure onTipSure) {
        return getSureTip(activity, App.INSTANCE.getString(R.string.libappCircleCheckUtil1), content, App.INSTANCE.getString(R.string.libtipUtil2), onTipSure);
    }

    public interface OnTipSure {
        void onSure();
    }
}
